import { combineReducers } from 'redux'  //引入构造函数
import {  //引入常量
  COMMON_NUM,
  COMMON_MESSAGE
} from "./type";

const stateData = { //初始值
  commonMessage: localStorage.getItem("commonMessage") ? JSON.parse(localStorage.getItem("commonMessage")) : {},
  commonNum: Number(localStorage.getItem("commonNum")) || 0
}

//Reducer函数
function commonMessageReducer(state = stateData.commonMessage, action) {
  // console.log(action, "commonMessageReducer")
  switch (action.type) {
    case COMMON_MESSAGE:
      const commonMessage = action.commonMessage || {}
      localStorage.setItem("commonMessage", JSON.stringify(commonMessage));
      return commonMessage
    default:
      return state;
  }
}

function commonNumReducer(state = stateData.commonNum, action) {
  // console.log(action, "commonNumReducer")
  switch (action.type) {
    case COMMON_NUM:
      const commonNum = action.commonNum || 0
      localStorage.setItem("commonNum", commonNum);
      return Number(commonNum)
    default:
      return state;
  }
}

//初始化模块
const common = combineReducers({
  commonMessageReducer,
  commonNumReducer
})

//导出模块
export default common